

package com.mt.restaurant.controller.material;

import com.mt.restaurant.entity.material.MaterialOrder;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.material.StoreMaterialEditDto;
import com.mt.restaurant.entity.material.StoreMaterial;
import com.mt.restaurant.service.material.StoreMaterialService;


@Api(tags = "仓库材料关系")
@RestController
@RequestMapping("/api/restaurant/material/StoreMaterial")
@CrossOrigin(allowCredentials = "true")
public class StoreMaterialController {
    private static Logger logger = LoggerFactory.getLogger(StoreMaterialController.class);


    @Autowired
    private StoreMaterialService storeMaterialService;

    /**
     * 根据分页参数查询仓库材料关系集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("分页查询仓库材料关系")
    @ApiPageParam
    @PostMapping("/findStoreMaterials")
    public PageResultDTO findStoreMaterials(@RequestBody PageDTO pageDTO) {
        return this.storeMaterialService.findStoreMaterials(pageDTO);
    }

    /**
     * 根据ID查询指定的仓库材料关系
     *
     * @param storeMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("根据id查询仓库材料关系")
    @ApiPageParam
    @PostMapping("/findStoreMaterial")
    public StoreMaterial findStoreMaterial(@RequestParam Long storeMaterialId) {
        return this.storeMaterialService.findStoreMaterial(storeMaterialId);
    }

    /**
     * 根据ID查询指定的仓库材料关系(包含外键名称)
     *
     * @param storeMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("根据ID查询指定的仓库材料关系(包含外键名称)")
    @PostMapping("/findStoreMaterialForView")
    public StoreMaterial findStoreMaterialForView(@RequestParam Long storeMaterialId) {
        return this.storeMaterialService.findStoreMaterialWithForeignName(storeMaterialId);
    }

    /**
     * 根据ID查询指定的仓库材料关系(包含仓库材料关系和外键名称)
     *
     * @param storeMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("根据ID查询指定的仓库材料关系(包含仓库材料关系和外键名称)")
    @PostMapping("/findStoreMaterialForEdit")
    public StoreMaterialEditDto findStoreMaterialForEdit(@RequestParam Long storeMaterialId) {
        StoreMaterialEditDto storeMaterialEditDto = new StoreMaterialEditDto();
        storeMaterialEditDto.setStoreMaterial(this.storeMaterialService.findStoreMaterialWithForeignName(storeMaterialId));

        this.prepareStoreMaterialEditDto(storeMaterialEditDto);

        return storeMaterialEditDto;
    }

    /**
     * 根据ID查询指定的仓库材料关系(只提取ID 和 Name)
     *
     * @param storeMaterialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("根据ID查询指定的仓库材料关系(只提取ID 和 Name)")
    @PostMapping("/findStoreMaterialsWithIdNameById")
    public StoreMaterial findStoreMaterialsWithIdNameById(@RequestParam Long storeMaterialId) {
        return this.storeMaterialService.findStoreMaterialsWithIdNameById(storeMaterialId);
    }

    /**
     * 根据名称查询仓库材料关系集合(只提取ID 和 Name)
     *
     * @param storeMaterialName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:view')")
    @ApiOperation("根据名称查询仓库材料关系集合(只提取ID 和 Name)")
    @PostMapping("/findStoreMaterialsWithIdNameByName")
    public List<StoreMaterial> findStoreMaterialsWithIdNameByName(@RequestParam String storeMaterialName) {
        return this.storeMaterialService.findStoreMaterialsWithIdNameByName(storeMaterialName);
    }


    /**
     * 创建新的仓库材料关系
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:add')")
    @ApiOperation("创建新的仓库材料关系")
    @PostMapping("/createStoreMaterial")
    public StoreMaterialEditDto createStoreMaterial() {
        StoreMaterialEditDto storeMaterialEditDto = new StoreMaterialEditDto();
        storeMaterialEditDto.setStoreMaterial(new StoreMaterial());

        this.prepareStoreMaterialEditDto(storeMaterialEditDto);
        return storeMaterialEditDto;
    }

    private void prepareStoreMaterialEditDto(StoreMaterialEditDto storeMaterialEditDto) {
    }

    /**
     * 新增保存仓库材料关系
     *
     * @param operation     操作类型
     * @param storeMaterial 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:add')")
    @ApiOperation("新增保存仓库材料关系")
    @PostMapping("/saveStoreMaterial")
    public StoreMaterial saveStoreMaterial(@RequestParam Integer operation, @RequestBody StoreMaterial storeMaterial) {
        return this.storeMaterialService.saveStoreMaterial(operation, storeMaterial);
    }

    /**
     * 修改保存仓库材料关系
     *
     * @param storeMaterial 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:update')")
    @ApiOperation("修改保存仓库材料关系")
    @PostMapping("/updateStoreMaterial")
    public StoreMaterial updateStoreMaterial(@RequestParam Integer operation, @RequestBody StoreMaterial storeMaterial) {
        return this.storeMaterialService.updateStoreMaterial(operation, storeMaterial);
    }

    /**
     * 根据订单入库修改仓库材料数量
     *
     * @param materialOrderList
     * @return
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:update')")
    @ApiOperation("根据订单入库修改仓库材料数量")
    @PostMapping("/updateStoreMaterialByOrder")
    public StoreMaterial updateStoreMaterialByOrder(@RequestBody List<MaterialOrder> materialOrderList) {
        return this.storeMaterialService.updateStoreMaterialByOrder(materialOrderList);
    }

    /**
     * 根据ID删除仓库材料关系
     *
     * @param storeMaterialId ID
     */
    @PreAuthorize("hasAuthority('restaurant:material:StoreMaterial:remove')")
    @ApiOperation("根据ID删除仓库材料关系")
    @PostMapping("/deleteStoreMaterial")
    public void deleteStoreMaterial(@RequestParam Long storeMaterialId) {
        this.storeMaterialService.deleteStoreMaterial(storeMaterialId);
    }


}

